## Script (Python) "Take Quiz"
##title=Take Quiz at bottom.
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=
##
request = context.REQUEST
form = request.form
questions = context.listQuestions(skiplabels=1)

member = context.portal_membership.getAuthenticatedMember()
id = context.getUniqueId(member.getId(), request)

for q in questions:
    answer = q.field.widget.process_form(context,q.field,form)[0]
    q.addAnswer(id, answer)
    if q.isCorrectAnswer( answer ):
        state.setError(q.id,'Incorrect Answer.  Try again.')

context.markTaken(member.getId(), id)
if state.getErrors():
    return state.set(status='mistakes',portal_status_message='Please correct your answers and try again.')
else:
    return state.set(status='success', 
                 portal_status_message='Thanks for taking the survey')